package com.lhr.common.domain;

import java.util.Collection;

public class Pagination<T> extends AbstractBean {

	private static final long serialVersionUID = 1L;
	// 默认的分页显示条数
	public static final int DEFAULT_PAGE_ITEM = 10;
	// 每页显示数据条数
	private int pageItem;
	// 总记录数
	private int totalCount;
	// 总页数
	private int pageCount;
	// 当前页
	private int pageIndex;
	// 是否存在下一页
	private boolean isNextPage;
	// 是否从在上一页
	private boolean isPreviousPage;
	// 从第几条开始显示
	private int skipResults;
	// 结束(应用在集合分页)
	private int endResults;
	// 分页数据存放
	private Collection<T> data;

	public Pagination(int totalCount, int pageIndex) {
		setProperty(totalCount, pageIndex, DEFAULT_PAGE_ITEM);
	}

	public Pagination(int totalCount, int pageIndex, int pageItem) {
		setProperty(totalCount, pageIndex, pageItem);
	}

	private void setProperty(int totalCount, int pageIndex, int pageItem) {
		// 显示行数
		if (pageItem < 1)
			this.pageItem = DEFAULT_PAGE_ITEM;
		else
			this.pageItem = pageItem;
		// 总记录数
		this.totalCount = totalCount;
		// 总页数
		if (this.totalCount == 0)
			this.pageCount = 1;
		else if (this.totalCount % this.pageItem == 0)
			this.pageCount = this.totalCount / this.pageItem;
		else
			this.pageCount = (this.totalCount / this.pageItem) + 1;
		// 当前页数
		if (pageIndex <= 1)
			this.pageIndex = 1;
		else if (pageIndex >= this.pageCount)
			this.pageIndex = this.pageCount;
		else
			this.pageIndex = pageIndex;
		// 是否存在下一页
		this.isNextPage = this.pageIndex < this.pageCount;
		// 是否存在上一页
		this.isPreviousPage = this.pageIndex > 1;
		// 从第几条开始显示
		this.skipResults = ((this.pageIndex - 1) * this.pageItem);
		// 从第几条结束
		this.endResults = this.skipResults + this.pageItem;
		if (this.endResults > this.totalCount)
			this.endResults = this.totalCount;
	}

	public boolean isNextPage() {
		return this.isNextPage;
	}

	public boolean getIsNextPage() {
		return this.isNextPage;
	}

	public boolean isPreviousPage() {
		return this.isPreviousPage;
	}

	public boolean getIsPreviousPage() {
		return this.isPreviousPage;
	}

	public int getSkipResults() {
		return skipResults;
	}

	public int getPageItem() {
		return pageItem;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public int getPageCount() {
		return pageCount;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public int getEndResults() {
		return endResults;
	}

	public Collection<T> getData() {
		return data;
	}

	public void setData(Collection<T> data) {
		this.data = data;
	}
}
